/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess.message.wechat.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.hyts.dess.message.wechat.domain.WechatMessageRequest;

import java.util.concurrent.TimeUnit;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess.message.wechat.service
 * @author:LiBo/Alex
 * @create-date:2020-08-06 10:16
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public interface WechatApiService {


    String ERROR_CODE_KEY = "errcode";

    String ACCESS_TOKEN_KEY = "access_token";

    String DEFAULT_SPLIT_TOKEN = "#";

    /**
     * 直接获取相关的accessToken数值
     * @return
     */
    String getAccessToken();

    /**
     * 获取相关的accessToken数值
     * @param appId
     * @param secretKey
     * @return
     */
    JSONObject getAccessToken(String appId,String secretKey);

    /**
     * 模板发送消息信息数据控制
     * @param accessToken
     * @param wechatMessageRequest
     */
    JSONObject templateSend(String accessToken, WechatMessageRequest wechatMessageRequest);

    /**
     * 根据jscode编码获取相关的openId
     * @param js_code
     * @return
     */
    JSONObject getOpenIdByJSCode(String js_code);

    /**
     * 抛出异常信息处理操作
     * @param body
     * @return
     */
    default JSONObject throwErrorMessageIfExists(String body) {
        String callMethodName = (new Throwable()).getStackTrace()[1].getMethodName();
        JSONObject jsonObject = JSON.parseObject(body);
        if (jsonObject.containsKey(ERROR_CODE_KEY) && jsonObject.getIntValue(ERROR_CODE_KEY) > 0) {
            throw new RuntimeException(StrUtil.format("#WechatApi[{}] call error: {}", callMethodName, body));
        }
        return jsonObject;
    }

}
